//
//  DynamicArr.hpp
//  DataStructureC++
//
//  Created by zhangbaochuan on 2020/4/17.
//  Copyright © 2020 Geex. All rights reserved.
//

#ifndef DynamicArr_hpp
#define DynamicArr_hpp

#include <stdio.h>
#include <assert.h>
#include <iostream>

using namespace std;

template <typename T>
class MyVector {
public:
    T* data;
    int  capacity;  //储存数组中可以容纳的最大的元素个数
    int size;   //储存数组中元素的个数
    
    void resize(int newCapacity) {
        T *newData = new T[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        delete[] data;
        data = newData;
        capacity = newCapacity;
    }
    
public:
    
    MyVector() {
        capacity = 10;
        size = 0;
        data = new T[capacity];
    }
    
    ~MyVector() {
        delete [] data;
        
    }
    
    void push_back(T e) {
        if (size == capacity) {
            resize(2 * capacity);
        }
        data[size++] = e;
    }
    
    T pop_back() {
        assert( size > 0);
        size--;
        return data[size];
    }
};

//void testDynamic(std::string str);
inline void testDynamic(string str) {
    cout << str << "\n";
    MyVector<int> vec;
    for (int i = 0; i < 10; i++) {
        vec.push_back(i);
    }
    
    for (int i = 0; i< 10; i++) {
        cout << vec.data[i] << "     ";
    }
};

#endif /* DynamicArr_hpp */
